/*
 * Dust.cpp
 *
 *  Created on: Jul 13, 2009
 *      Author: scott
 */

#include "Dust.h"

irr::core::list<Dust*> Dust::dust;

Dust::Dust(World * world, SceneGraphNode * parent, const char * filename, bool ownsChildren) :
	MeshObject(parent, filename, ownsChildren)
{
	// generate a body
	body = dBodyCreate(world->getWorld());

	dBodySetData(body, (void*) 2);
	dMass m;
	dMassSetBox(&m, 100, 0.2, 0.2, 0.2);
	dMassAdjust(&m, 100);
	dBodySetMass(body,&m);

	dGeomID box = dCreateBox(0,0.1,0.1,0.1);

	box = dCreateBox(0,0.2,0.2,0.2);

	dGeomSetBody(box, body);
	dBodySetPosition(body, 0, 0, 0);
	irr::core::matrix4 rot;
	rot.setRotationDegrees(irr::core::vector3df(0, 0, 0));
	float rotBuffer[16];
	dBodySetRotation(body,(const dReal*) World::opengl2OdeR(rotBuffer, rot.pointer()));
	dSpaceAdd(world->getSpace(), box);
	setScale(irr::core::vector3df(0.1, 0.1, 0.1));
	dust.push_back(this);
}

Dust::~Dust() {
	dGeomDestroy(box);
	dBodyDestroy(body);
	//dust.remove(this);
}

void Dust::update()
{
	float rotBuffer[16];
	const float * bodyTrans = dBodyGetPosition(body);
	irr::core::matrix4 bodyRot;

	bodyRot.setM(World::ode2Opengl(rotBuffer, bodyTrans, dBodyGetRotation(body)));
	setTranslation(irr::core::vector3df(bodyTrans[0], bodyTrans[1], bodyTrans[2]));
	setRotation(bodyRot.getRotationDegrees());
}

void Dust::reverseUpdate()
{
	irr::core::vector3df pos = getTranslation();
	dBodySetPosition(body, pos.X, pos.Y, pos.Z);
	irr::core::matrix4 rot;
	rot.setRotationDegrees(getRotation());
	float rotBuffer[16];
	dBodySetRotation(body,(const dReal*) World::opengl2OdeR(rotBuffer, rot.pointer()));
}




